﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using League.DataAccess;

namespace League.Business
{
    public class LeagueTeamRankingController
    {
        private static LeagueMatchController leagueMatchController = new LeagueMatchController();

        public static List<LeagueTeamRanking> CountRankingByRoundNumber(string leagueId, int roundNumber)
        {
            List<LeagueTeamRanking> result = new List<LeagueTeamRanking>();


            List<LeagueMatch> leagueMatchSet = leagueMatchController.GetMatchByRoundNumber(leagueId, roundNumber);

            List<LeagueTeamRanking> tempTeamRanking = new List<LeagueTeamRanking>();

            foreach (LeagueMatch leagueMatch in leagueMatchSet)
            {
                List<LeagueTeamRanking> leagueTeamRankingSet =
                    GetLeagueTeamRankingByRoundNumber(leagueMatch.RoundNumber - 1);
                List<LeagueTeamData> matchData = LeagueTeamDataDataAccess.Find(leagueMatch.MatchID.ToString());

                #region home and visiting ranking build

                LeagueTeamRanking homeRanking =
                    leagueTeamRankingSet.Where(l => l.TeamID == leagueMatch.HomeTeamID).FirstOrDefault();
                LeagueTeamRanking visitRanking =
                    leagueTeamRankingSet.Where(l => l.TeamID == leagueMatch.VisitingTeamID).FirstOrDefault();

                if (homeRanking == null)
                {
                    homeRanking = new LeagueTeamRanking();
                }

                if (visitRanking == null)
                {
                    visitRanking = new LeagueTeamRanking();
                }

                homeRanking.RoundNumber = leagueMatch.RoundNumber;
                homeRanking.TeamID = leagueMatch.HomeTeamID;
                homeRanking.LeagueID = leagueMatch.LeagueID;

                visitRanking.RoundNumber = leagueMatch.RoundNumber;
                visitRanking.TeamID = leagueMatch.VisitingTeamID;
                visitRanking.LeagueID = leagueMatch.LeagueID;


                if (matchData.Count == 2)
                {
                    homeRanking.Goal += matchData.Where(m => m.TeamID == leagueMatch.HomeTeamID).FirstOrDefault().Goal;
                    visitRanking.Goal += matchData.Where(m => m.TeamID == leagueMatch.VisitingTeamID).FirstOrDefault().Goal;
                    homeRanking.Lose += visitRanking.Goal;
                    visitRanking.Lose += homeRanking.Goal;

                    homeRanking.TeamMatchCount += 1;
                    visitRanking.TeamMatchCount += 1;

                    if (leagueMatch.MatchResult == 3)
                    {
                        homeRanking.TeamPoint += 3;
                        visitRanking.TeamPoint += 0;
                        homeRanking.MatchWin += 1;
                        visitRanking.MatchLose += 1;
                    }
                    else if (leagueMatch.MatchResult == 1)
                    {
                        homeRanking.TeamPoint += 1;
                        visitRanking.TeamPoint += 1;
                        homeRanking.MatchDraw += 1;
                        visitRanking.MatchDraw += 1;
                    }
                    else if (leagueMatch.MatchResult == 0)
                    {
                        homeRanking.TeamPoint += 0;
                        visitRanking.TeamPoint += 3;
                        homeRanking.MatchLose += 1;
                        visitRanking.MatchWin += 1;
                    }

                    tempTeamRanking.Add(homeRanking);
                    tempTeamRanking.Add(visitRanking);
                }
                #endregion
            }

            tempTeamRanking = tempTeamRanking.OrderByDescending(o => o.TeamPoint).ToList();

            for (int i = 1; i <= tempTeamRanking.Count; i++)
            {
                tempTeamRanking[i - 1].TeamRanking = i;
                result.Add(tempTeamRanking[i - 1]);
            }
            return result;
        }

        public static List<LeagueTeamRanking> GetLeagueTeamRankingByRoundNumber(int roundNumber)
        {
            return LeagueTeamRankingDataAccess.GetTeamRankingByRoundNumber(roundNumber);
        }

        public static int InsertOrUpdate(LeagueTeamRanking leagueTeamRanking)
        {
            int result = 0;
            if (LeagueTeamRankingDataAccess.Find(leagueTeamRanking.LeagueID.ToString(), leagueTeamRanking.RoundNumber,
                leagueTeamRanking.TeamID.ToString()) != null)
            {
                result = Update(leagueTeamRanking);
            }
            else
            {
                result = Insert(leagueTeamRanking);
            }
            return result;
        }

        public static int Update(LeagueTeamRanking leagueTeamRanking)
        {
            return LeagueTeamRankingDataAccess.Update(leagueTeamRanking);
        }

        public static int Insert(LeagueTeamRanking leagueMatch)
        {
            return LeagueTeamRankingDataAccess.Insert(leagueMatch);
        }
    }
}
